package com.gomore.opple.module.main.home.adapter;

import android.content.Context;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.gomore.opple.OppleApplication;
import com.gomore.opple.R;
import com.gomore.opple.data.DataRepository;
import com.gomore.opple.model.EventQueryShoppingCar;
import com.gomore.opple.rest.consumer.ConsumerDesigner;
import com.gomore.opple.service.ConsumerClickListener;
import com.gomore.opple.utils.DateUtil;
import com.gomore.opple.utils.DensityUtil;
import com.gomore.opple.utils.TextUtil;
import com.gomore.opple.web.cgform.consumer.entity.TOConsumerEntity;
import com.gomore.opple.widgets.XCRoundRectImageView;
import com.gomore.opple.widgets.adapter.CommonAdapter;
import com.gomore.opple.widgets.adapter.ViewHolder;

import java.util.List;

import de.greenrobot.event.EventBus;


/**
 * Created by asus on 2017/3/20.
 */
public class ConsumerAdapter extends CommonAdapter<ConsumerDesigner> {
    Context mContext;
    int height;
    DataRepository dataRepository;
    ConsumerClickListener consumerClickListener;

    public void setConsumerClickListener(ConsumerClickListener consumerClickListener1) {
        consumerClickListener = consumerClickListener1;
    }

    public ConsumerAdapter(Context context, int layoutId, List<ConsumerDesigner> messageList) {
        super(context, layoutId, messageList);
        mContext = context;
        dataRepository = OppleApplication.getInstance().getRepositoryComponent().getDataRepository();
        height = DensityUtil.dip2px(mContext, 80);
    }

    @Override
    protected void convert(ViewHolder holder, final ConsumerDesigner consumerDesigner, final int position) {
        XCRoundRectImageView customer_icon = holder.getView(R.id.customer_icon);
        final ImageView consumer_select = holder.getView(R.id.consumer_select);
        TextView consumerName = holder.getView(R.id.customer_name);
        TextView customer_emplee = holder.getView(R.id.customer_emplee);
        TextView customer_mobile = holder.getView(R.id.customer_mobile);
        TextView customer_address = holder.getView(R.id.customer_address);
        TextView customer_time = holder.getView(R.id.customer_time);
        final CheckBox select = holder.getView(R.id.select);
        final TextView select_text = holder.getView(R.id.select_text);
        final TOConsumerEntity consumerEntity = consumerDesigner.getConsumer();
        //如果有选中
        if (dataRepository.getConsumer() != null) {
            TOConsumerEntity consumerEntity1 = dataRepository.getConsumer();
            if (TextUtil.isValid(consumerEntity1.getId()) &&
                    consumerEntity1.getId().equals(consumerEntity.getId())) {
                consumer_select.setVisibility(View.VISIBLE);
                select.setChecked(true);
                select_text.setTextColor(mContext.getResources().getColor(R.color.blue));
            } else {
                consumer_select.setVisibility(View.GONE);
                select.setChecked(false);
                select_text.setTextColor(mContext.getResources().getColor(R.color.gray_text));
            }
        }
        String url = "demo";
        if (consumerEntity.getPicture() != null) {
            url = consumerEntity.getPicture();
        }
        Glide.with(mContext)
                .load(url)
                .asBitmap()
                .override(height, height)
                .placeholder(R.mipmap.consumer_icon)
                .into(customer_icon);
        if (consumerEntity.getName() != null) {
            consumerName.setText(consumerEntity.getName());
        }

        if (consumerEntity.getCreateName() != null) {
            customer_emplee.setText(consumerEntity.getCreateName());
        }

        if (consumerEntity.getMobile() != null) {
            customer_mobile.setText(consumerEntity.getMobile());
        }

        if (consumerEntity.getAddress() != null) {
            String address;
            if (consumerEntity.getProvince() != null) {
                address = consumerEntity.getProvince() + consumerEntity.getCity() +
                        consumerEntity.getCountry() + consumerEntity.getAddress();
            } else {
                address = consumerEntity.getAddress();
            }
            customer_address.setText(address);
        }

        if (consumerEntity.getLastVisitedTime() != null) {
            customer_time.setText(DateUtil.formatDate(consumerEntity.getLastVisitedTime(), DateUtil.DATE_FORMATTER_1));
        }
        //选择框的点击事件
        select.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (select.isChecked()) {
                    dataRepository.saveConsumer(consumerEntity);
                    select_text.setTextColor(mContext.getResources().getColor(R.color.blue));
                    consumerClickListener.select(consumerEntity.getId(), true);
                } else {
                    dataRepository.clearConsumer();
                    select_text.setTextColor(mContext.getResources().getColor(R.color.gray_text));
                    consumer_select.setVisibility(View.GONE);
                    consumerClickListener.select(consumerEntity.getId(), false);
                }
                EventQueryShoppingCar eventQueryShoppingCar = new EventQueryShoppingCar();
                eventQueryShoppingCar.setIsGetShoppingCar(true);
                EventBus.getDefault().post(eventQueryShoppingCar);
            }
        });
    }
}